<?php

require_once 'Nine/Model.php';
class Models_Folder extends Nine_Model
{ 
    
    protected $_primary = 'folder_id';
   
    public function __construct($config = array())
    {
        $this->_name = $this->_prefix . 'folder';
        return parent::__construct($config); 
    }

    /**
     * Get all product with conditions
     * 
     * @param $condition
     * @param $order
     * @param $count
     * @param $offset
     */
   
    
    public function getFolderById( $id ) 
    {
    	$select = $this->select()
    			->where('folder_id=?',$id);
    	
    	return @reset($this->fetchAll($select)->toArray());
    }
    
    public function getAllFolders($condition = array(), $order = null, $count = null, $offset = null)
    {
    	$select = $this->select()
    	->setIntegrityCheck(false)
    	->from(array('c' => $this->_name))
    	->join(array('cc' => $this->_prefix . 'content_category'), 'c.subject_id = cc.content_category_id', array('cname' => 'name'))
    	->order($order)
    	->limit($count, $offset);
    	/**
    	 * Conditions
    	 */
    	if (null != @$condition['keyword']) {
    		$sql = "SELECT folder_id FROM {$this->_name} WHERE " . $this->getAdapter()->quoteInto('title LIKE ?', "%{$condition['keyword']}%");
    		$ids = $this->_db->fetchAll($sql);
    		if (empty($ids)) {
    			return array();
    		}
    		$idString = '';
    		foreach ($ids as $row) {
    			$idString .= $row['folder_id'] . ',';
    		}
    		/**
    		 * Add to select object
    		 */
    		$select->where('c.folder_id IN (' . trim($idString, ',') .')');
    	}
    	if (null != @$condition['subject_id']) {
    		 
    		 
    		/**
    		 * Add to select object
    		 */
    		$select->where('c.subject_id IN (' . @$condition['subject_id'] .')');
    	}
    
    	return $this->fetchAll($select)->toArray();
    } 
      
     public function getFolderByCatId( $gid )
    {
    	$select = $this->select()
    	->where('subject_id=?',$gid);
    	 
    	return $this->fetchAll($select)->toArray();
    }
    
}